<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
	var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
 
        $mainframe = JFactory::getApplication();
 
        /* Get pagination request variables */
        $limit = $mainframe->getUserStateFromRequest('com_msbanner.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        /* In case limit has been changed, adjust it */
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    
    /* get all records coma */
	function getAll($list = null){

        $qr = "SELECT * FROM banner";
        $where = array();
       /* search */
       
       if($list['filter_search'] != null){
            $where[] = "BANNER_IMG LIKE '%$list[filter_search]%' OR BANNER_CODE = '$list[filter_search]'";
       } 
       
       if($where){
            $where = ' WHERE '.implode(' AND ', $where);
        }else{
            $where = null;
        }
       /* get total number record */
       $this->_total = $this->getTotal($where);
       
       $qr .= $where;
       /* order by */
       $qr .= " ORDER BY BANNER_CODE DESC"; 
       
       /* pagination */
       $qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
       /* load data */
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
 
       return $result;
       
	}

    /* get total records coma */
    function getTotal($where = null){
        /* Load the content if it doesn't already exist */
        if ($this->_total === null) {
            $query = "SELECT count(*) FROM banner $where";
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        
        return $this->_total;
    } 
    
    /* get pagination */
    function getPagination(){
        /* Load the content if it doesn't already exist */
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    
    /* save */
     public function save($data,$addNew){

      if($addNew == true){
        $qr = "INSERT INTO banner (BANNER_CODE,BANNER_IMG,BANNER_URL) VALUES ('$data[BANNER_CODE]', '$data[BANNER_IMG]','$data[BANNER_URL]')";
          
        $this->_db->setQuery($qr);
        if($this->_db->query())
            return true;
        else
            return false;
      }
      else{
        $qr = "UPDATE banner SET BANNER_IMG = '$data[BANNER_IMG]'
                          ,BANNER_URL = '$data[BANNER_URL]'
                          WHERE BANNER_CODE = '$data[BANNER_CODE]'";
          
        $this->_db->setQuery($qr);
        if($this->_db->query())
            return true;
        else
            return false;
      }
     }

    /* remove club items */
    public function remove($cid){
        $cid = implode(',', $cid);
    
        $query = "DELETE FROM banner WHERE BANNER_CODE IN($cid)";
  
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    //Get Image name from id
    public function getImgFromBannerCode($cid){
        $qr = "SELECT BANNER_IMG FROM banner WHERE BANNER_CODE = '$cid'";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
       return $result->BANNER_IMG;
    }

    /**
  *Check QUES_CODE exist
  */
    function checkExistID($cid){
        $qr = "SELECT BANNER_CODE FROM banner WHERE BANNER_CODE = $cid";
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObject();
        if(!empty($result)){ 
            return true;
        }
        return false;
    }

    function load($cid){
        $qr = "SELECT * FROM banner WHERE BANNER_CODE = $cid";
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObject();
        return $result;
    }
}
